﻿@{
    ViewData["Title"] = "EliminationManagement";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<link href="~/layui/css/layui.css" rel="stylesheet" type="text/css" />
<div style="margin:20px 20px">
    <form class="layui-form layui-row layui-col-space0">
        <div class="layui-col-md3">
            <div class="layui-form-item">
                <label class="layui-form-label">淘汰日期:</label>
                <div class="layui-input-block layui-input-wrap">
                    <input type="text" autocomplete="off" name="startSheepElimination" id="startSheepElimination" lay-verify="date" class="layui-input layui-input-inline" placeholder="开始日期" style="width:128px">
                    <div class="layui-form-mid" style="margin:0px 10px 0px 3px">-</div>
                    <input type="text" autocomplete="off" name="endSheepElimination" id="endSheepElimination" lay-verify="date" class="layui-input layui-input-inline" placeholder="结束日期" style="width:128px">
                </div>
            </div>
        </div>
        <div class="layui-col-md2">
            <div class="layui-form-item">
                <label class="layui-form-label">羊只耳号:</label>
                <div class="layui-input-block">
                    <input type="text" name="earNumber" id="earNumber" placeholder="请输入" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-col-md2">
            <div class="layui-form-item">
                <label class="layui-form-label">淘汰原因:</label>
                <div class="layui-input-block">
                    <select name="eliminationCause" id="eliminationCause">
                        <option value="">请选择淘汰原因</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-md2">
            <div class="layui-form-item">
                <label class="layui-form-label">是否离场:</label>
                <div class="layui-input-block">
                    <select name="isLeave" id="isLeave">
                        <option value="">请选择是否离场</option>
                        <option value="已离场">已离场</option>
                        <option value="在场">在场</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-md2" style="margin-left:36px">
            <div class="layui-form-item">
                <div class="layui-btn-container layui-col-xs12">
                    <button type="button" class="layui-btn" id="searchBtn" style="background-color:red;border-radius:5px">查询</button>
                    <button type="reset" class="layui-btn layui-btn-primary" style="border-radius:5px">重置</button>
                </div>
            </div>
        </div>
    </form>
    <table class="layui-hide" id="ID-table-demo-setRowChecked" lay-filter="test"></table>
    <script type="text/html" id="demo-toolbar-setRowChecked">
        <div class="layui-btn-container">
          <button class="layui-btn" style="background-color:red; border-radius:5px"  onclick="location.href='/SheepManagement/EliminationManagementAdd'">
              新增
              </button>
          <button class="layui-btn layui-btn-disabled" id="delete" style="background-color:red; border-radius:5px" onclick="delCheckData()" disabled="disabled">
              删除
              </button>
          <button class="layui-btn layui-btn-disabled" id="audit" style="background-color:red; border-radius:5px" onclick="auditData()" disabled="disabled">
              审核
              </button>
        </div>
    </script>
    <div id="pagesbox"></div>
    <script>
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laypage = layui.laypage;

        layui.use(['laydate'], function () {
            var laydate = layui.laydate;

            laydate.render({
                elem: '#startSheepElimination',
            });
            laydate.render({
                elem: '#endSheepElimination'
            });
        })

        // 可视耳号超链接事件
        function addLink(data) {
            var addLink = data.earNumber;
            if ('' == addLink || null == addLink || undefined == addLink) {
                return '';
            }
            if (addLink.length > 0) {
                return '<a class="layui-table-link" href="javascript:void(0);" lay-event="link"  onclick="jumpArchivesSheepDetailPage(' + data.id + ')" >' + data.earNumber + '</a>';
            }
        }

        function jumpArchivesSheepDetailPage(id) {
            location.href = '/IAMH/ArchivesSheepDetailPage?id=' + id;
        }

        // 淘汰原因
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/SheepManagementGetRout/api/SheepManagement/GetEliminationCause', // 后端枚举数据接口地址
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    $.each(res, function (index, item) {
                        // 使用正确的字段添加选项到下拉框中
                        var option = $('<option>').val(item.value).text(item.value);
                        $('#eliminationCause').append(option);
                    });
                    // 手动触发 layui 组件的渲染
                    layui.form.render('select');
                },
                error: function () {
                    // 处理 AJAX 请求失败的情况
                    console.error('Failed to fetch data for dropdown.');
                }
            });
        });


        var pageSize = 15; // 每页显示条数
        //获取列表
        function getListData(pageIndex) {
            //ajax后台交互
            $.ajax({
                //url: 'http://localhost:5282/api/Archives/ArchivesPageList',
                url: 'http://10.31.60.18:8000/SheepManagementGetRout/api/SheepManagement/GetEliminationPageList',
                type: 'get',
                dataType: 'json',
                //参数
                data: {
                    Size: pageSize,
                    Index: pageIndex,
                    EarNumber: $("#earNumber").val() || "",
                    StartSheepElimination: $("[name=startSheepElimination]").val() || "",
                    EndSheepElimination: $("[name=endSheepElimination]").val() || "",
                    EliminationCause: $("[name=eliminationCause]").val() || "",
                    IsLeave: $("[name=isLeave]").val() || "",
                },
                success: function (res) {

                    console.log(res);
                    //渲染表格数据
                    layui.use(['table', 'laypage'], function () {
                        var table = layui.table;
                        var laypage = layui.laypage;

                        table.render({
                            //挂载的表格id
                            elem: '#ID-table-demo-setRowChecked',
                            cols: [[
                                { type: 'checkbox', width: 60, fixed: 'center', unresize: true, align: 'center' },
                                { type: 'numbers', title: '序号', width: 60, fixed: 'center', unresize: true, align: 'center' },
                                { field: 'eliminationDate', title: '淘汰日期', width: 120, align: 'center', sort: true, templet: function (data) { return data.eliminationDate.substring(0, 10) } },
                                { field: 'earNumber', title: '羊只耳号', width: 120, align: 'center', templet: addLink, sort: true },
                                { field: 'eliminationCause', title: '淘汰原因', width: 120, align: 'center' },
                                { field: 'isLeave', title: '是否离场', width: 120, align: 'center' },
                                { field: 'eliminationMeasures', title: '处理措施', width: 120, align: 'center' },
                                { field: 'sheepBreed', title: '品种', width: 120, align: 'center' },
                                {
                                    field: 'sheepGender', title: '性别', width: 120, align: 'center',
                                    templet: function (data) {
                                        if (data.sheepGender == true) return '公';
                                        if (data.sheepGender == false) return '母';
                                    },
                                },
                                { field: 'reproductiveState', title: '繁殖状态', width: 120, align: 'center', },
                                { field: 'eliminationMonthAge', title: '淘汰月龄', width: 120, align: 'center' },
                                { field: 'eliminationDayAge', title: '淘汰日龄', width: 120, align: 'center' },
                                { field: 'cottageId', title: '淘汰栋舍', width: 120, align: 'center' },
                                { field: 'field', title: '淘汰栏位', width: 120, align: 'center' },
                                { field: 'sheepBir', title: '出生日期', width: 120, align: 'center', templet: function (data) { return data.sheepBir.substring(0, 10) } },
                                { field: 'createBy', title: '创建人', width: 120, align: 'center' },
                                { field: 'createDate', title: '创建时间', width: 120, align: 'center', sort: true },
                                { field: 'updateBy', title: '修改人', width: 120, align: 'center' },
                                { field: 'updateDate', title: '修改时间', width: 120, align: 'center' },
                                { field: 'examineBy', title: '审核人', width: 120, align: 'center' },
                                { field: 'examineDate', title: '审核时间', width: 120, align: 'center' },
                                { field: 'documentNumber', title: '单据号', width: 120, align: 'center' },
                                { title: '操作', width: 120, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true },
                            ]],
                            toolbar: '#demo-toolbar-setRowChecked',
                            data: res.datas
                        });

                        //分页
                        laypage.render({
                            //挂载的分页id
                            elem: 'pagesbox',
                            count: res.totalCount,
                            limit: pageSize,
                            curr: pageIndex,
                            limits: [15, 20, 25],
                            layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
                            jump: function (obj, first) {
                                if (!first) {
                                    pageSize = obj.limit;
                                    getListData(obj.curr);
                                }
                            }
                        });
                    });
                }
            });
        }

        // 查询方法
        $('#searchBtn').on('click', function () {
            getListData(1);
        });

        // 单删除
        function deleteData(id) {
            $.ajax({
                url: `http://10.31.60.18:8000/SheepManagementRout/api/SheepManagement/DeleteEliminationManagement?Id=${id}`,
                type: 'delete',
                success: function (res) {
                    if (res > 0) {
                        layer.msg('删除成功');
                        getListData(1);
                    } else {
                        layer.msg('删除失败');
                    }
                },
                error: function () {
                    layer.msg('删除请求失败');
                }
            });

        }

        // 批量删除
        function delCheckData() {
            var checkStatus = table.checkStatus('ID-table-demo-setRowChecked'),
                data = checkStatus.data,
                ids = [];
            if (data.length > 0) {
                for (var i in data) {
                    ids.push(data[i].id);
                }
                layer.confirm('确定删除选中的记录？', { icon: 3, title: '提示信息' }, function (index) {
                    $.ajax({
                        url: `http://10.31.60.18:8000/SheepManagementRout/api/SheepManagement/BatchDeleteEliminationManagement?Ids=${ids}`,
                        type: 'delete',
                        success: function (res) {
                            if (res > 0) {
                                layer.msg("删除成功");
                                getListData(1);
                            } else {
                                layer.msg("删除失败");
                            }
                        },
                        error: function () {
                            layer.msg('请求失败');
                        }
                    });
                    layer.close(index);
                });
            } else {
                layer.msg("请选择需要删除的记录");
            }
            console.log(checkStatus);
        }

        // 审核
        function auditData() {
            var checkStatus = table.checkStatus('ID-table-demo-setRowChecked'),
                data = checkStatus.data,
                ids = [];
            if (data.length > 0) {
                for (var i in data) {
                    ids += data[i].id + ",";
                }
                ids = ids.substring(0, ids.length - 1);
                var formData = {
                    Ids: ids,
                };
                layer.confirm('确定审核选中的记录？', { icon: 3, title: '提示信息' }, function (index) {
                    $.ajax({
                        url: `http://10.31.60.18:8000/SheepManagementRout/api/SheepManagement/AuditEliminationManagement`,
                        type: 'put',
                        data: JSON.stringify(formData),
                        contentType: 'application/json',
                        success: function (res) {
                            if (res > 0) {
                                layer.msg("审核成功");
                                getListData(1);
                            } else {
                                layer.msg("审核失败");
                            }
                        },
                        error: function () {
                            layer.msg('审核请求失败');
                        }
                    });
                    layer.close(index);
                });
            } else {
                layer.msg("请选择需要审核的记录");
            }
            console.log(checkStatus);
        }

        // 监听工具条，实现删除功能
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                window.location.href = `/SheepManagement/EliminationManagementEdit?earNumber=${data.earNumber}`;
            }
            if (obj.event == 'delete') {
                layer.confirm('确定删除吗？', function (index) {
                    deleteData(data.id); // 传递正确的数据项ID
                    layer.close(index);
                });
            }
        });

        // 触发表格复选框选择
        table.on('checkbox(test)', function (obj) {
            alert(1);
            var checkStatus = table.checkStatus('ID-table-demo-setRowChecked');
            var checkedData = checkStatus.data;
            // 根据多选列是否有值被选中来启用或禁用头部 button
            if (checkedData.length > 0) {
                $('#delete').attr('class', 'layui-btn');
                $('#audit').attr('class', 'layui-btn');
                $('#delete').removeAttr('disabled');
                $('#audit').removeAttr('disabled');
            } else {
                $('#delete').attr('class', 'layui-btn layui-btn-disabled');
                $('#audit').attr('class', 'layui-btn layui-btn-disabled');
            }
        });

        // 初始化加载数据
        getListData(1);


    </script>
    <script type="text/html" id="barDemo">
        <div>
          <a class="layui-table-link" style="color:dodgerblue;margin-right:15px" href="javascript:void(0);" lay-event="edit">编辑</a>
          <a  class="layui-table-link"style="color:dodgerblue" href="javascript:void(0);" lay-event="delete">删除</a>
        </div>
    </script>
</div>







